home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / monax25 / parse.c < prev    next >
Text File  |  1987-10-18  |  9KB  |  422 lines

  1. /*  parse.c - Parser for Report tool.
  2.    This module is part of report.exe
  3.                   
  4.    Language = Microsoft C version 4.0
  5.  
  6.  
  7.    This source is distributed freely and may be copied and
  8.    redistributed with the following provisos:
  9.    
  10.            You may not sell it, nor may you charge for making 
  11.            copies beyond the actual cost of mailing and media.
  12.                       
  13.    Written by Skip Hansen WB6YMH and Harold Price NK6K.
  14.  
  15.    Feedback is desired.
  16.  
  17.    RCP/M (213) 541-2503 300/1200/2400 baud
  18.    or via packet WB6YMH @ WB6YMH-2 or 
  19.          NK6K @ NK6K
  20.  
  21.    Modification history:
  22.  
  23.     8/10/87         NK6K: Initial release.    
  24.     ver 1.0         
  25.  
  26.    10/18/87     NK6K: First general release.
  27.    ver 1.1
  28.   
  29.  
  30.     Notes:  This was a quick grab from something else that was
  31.         laying around, and has several hooks for things 
  32.         which aren't actually here.  On the other hand, it
  33.         does the job.  To add new commands, add the text to
  34.         parse.h and a new case in doit() below.
  35.  
  36. */
  37.  
  38.  
  39. #include "parse.h"
  40. #include <ctype.h>
  41. #include <stdio.h>
  42. #include   <dos.h>
  43.  
  44.  
  45. #define promptlen 4;
  46.  
  47. int ars,point,ppoint,i,tc;
  48. unsigned int pnum;
  49. int old_more;
  50.  
  51. char token[129];
  52. char buf[128];
  53. int ctype;
  54. int ltype;
  55. int first_com_g;
  56.       
  57. int tflag,ok,eh;
  58. #define true 1
  59. #define false 0
  60. int point;
  61.  
  62. enum ttypes { notoken,
  63.     alftoken,
  64.     numtoken,
  65.     qmarktoken,        /* ? */
  66.     cmarktoken,        /* ; */
  67.     qtmarktoken,        /* " */
  68.     errtoken}  typtoken;
  69.  
  70. char oplist[] = " ?;\"";
  71.  
  72. FILE *fin, *fout, *fopen();
  73. int in_open = 0;
  74. int out_open = 0;
  75.  
  76.  
  77.  
  78. /* global defs */
  79. char sel_call[11];
  80. int sel_flag=0;
  81. char sel_types[0]="CDFIT";
  82. unsigned long recnum;
  83.  
  84. int sp_skip(i)
  85. int i;
  86. {
  87. int looping;
  88.         looping = true;
  89.         point = i;
  90.         while (looping) {
  91.             if (point >=ars) looping = false;
  92.             else  if(buf[point]==' ') point++;
  93.               else looping = false;
  94.     }
  95.         return(point<ars);
  96. }
  97.  
  98.  
  99.  
  100. ptoken(npoint)
  101. int npoint;
  102. {
  103. int i,looping,itemp;
  104. char c;
  105.         memset(token,' ',127);
  106.         looping = sp_skip(npoint);
  107.         ppoint = point;
  108.         pnum = 0;
  109.         tc = -1;
  110.         typtoken = notoken;
  111. /* get token first */
  112. /* check for opcodes */
  113.     if (looping) {
  114.        typtoken=errtoken;        /* once started, it isn't notoken */
  115.        c = buf[point];
  116.        if(c=='?') {
  117.         tc++;
  118.         point++;
  119.         looping = false;
  120.         typtoken=qmarktoken;
  121.         }
  122.        else if(c==';') {
  123.         tc++;
  124.         point++;
  125.         looping = false;
  126.         typtoken=cmarktoken;
  127.         }
  128.        else if(c=='"') {
  129.         point++;
  130.         looping = true;    /* keep looping */
  131.         typtoken=qtmarktoken;
  132.         }
  133.        }    
  134.     if (!looping) token[tc]=c;    /* opcode */
  135.  
  136.     if (typtoken==qtmarktoken) {
  137.  
  138. /* quoted string */
  139.       while (looping) {
  140.         c = buf[point];
  141.            if (c=='"')     { /* check for double delimiter */
  142.         if (point+1>=ars) {
  143.             looping=false;
  144.             point++;   /* skip past ' */
  145.             }
  146.         else if (buf[point+1]=='"') {   /* put a ' into the token */
  147.             tc++;
  148.             if (tc<127) token[tc] = c;
  149.             point+=2;  /* skip past both ' */
  150.             }
  151.         else {
  152.             looping=false;
  153.             point++;     /* skip past closing ' */
  154.             }
  155.         }
  156.        else {
  157.                    tc++;
  158.                    if (tc<127) token[tc] = c;
  159.                    point++;
  160.                    if (point>=ars) {
  161.              looping = false;
  162.             typtoken=errtoken;  /* no trailing ' */
  163.             }
  164.                     }
  165.            } /* while looping */
  166.       }  /* end if qtmark */
  167.     else  if (looping) {
  168. /* alphanumeric token */
  169.         typtoken=alftoken;
  170.         while (looping) {
  171.             c = buf[point];
  172.                    if ((isalnum(c)) || (c=='_') || (c=='.') || (c=='-') ||
  173.             (c=='/')||(c=='\\')) {
  174.                         tc++;
  175.                             if (tc<127) token[tc] = c;
  176.                             point++;
  177.                             if (point>=ars) looping = false;
  178.                             }
  179.                     else if (NULL==strchr(oplist,c)) {
  180.                 looping = false;
  181.                 typtoken=errtoken;
  182.                 }
  183.             else looping = false;    
  184.                 }
  185.         }
  186.     token[tc+1]='\0';
  187. /* now characterize it */
  188.     if (typtoken==alftoken) {  /* alphanumeric, check for numeric */
  189.     typtoken = numtoken;
  190.       for (i=0;i<tc;i++) { 
  191.             if (!isdigit(token[i])) {
  192.             typtoken=alftoken;
  193.             break;
  194.             }
  195.         }
  196.     }
  197. /* convert numeric tokens */
  198.     pnum=0;
  199.     if (typtoken==numtoken) 
  200.         for(i=0;i<=tc;i++) pnum = pnum*10 + token[i]-'0';
  201.  
  202. }
  203.  
  204.  
  205. pcom()
  206. {
  207. int match, looking, i;
  208.         looking = true;
  209.         ctype = 1+ (int) c_fnull ;
  210.         while (comtext[ctype].c[0]!='\0' && (looking==1)) {
  211.             match = true;
  212.             i = 0;
  213.             while ((i <= tc) && (match==1)) {
  214.                match = (toupper(token[i]) == comtext[ctype].c[i]);
  215.                 i++;
  216.               }
  217.             if (match==1) looking = false;
  218.             else ctype++;
  219.           }
  220.       }
  221.  
  222. writedol(i)
  223. int i;
  224. {
  225.  int j;
  226.         i = i + promptlen;
  227.         for (j=0;j<i;j++) cprintf(" ");
  228.         cprintf("^");
  229. }
  230.  
  231. writeeh(i)
  232. int i;
  233. {
  234.         writedol(i);
  235.         cprintf(" Error");
  236.     pbreak();
  237. }
  238.  
  239. peh()
  240. {
  241.     eh=true;
  242.     ok=false;
  243. }
  244.  
  245. parse(flag,s)
  246. int flag;            /* command line flag */
  247. char *s;
  248. {
  249.     ok = true;
  250.     eh = false;
  251.     if (flag) 
  252.       strcpy (buf,s);
  253.     else gets(buf);
  254.     ars = strlen(buf);
  255.                 
  256.         ptoken(0);
  257.     while ((ok) && (typtoken!=notoken)){
  258.         if (typtoken==qmarktoken) {
  259.             ctype=(int) c_help;
  260.             doit();
  261.             }
  262.         else if ((typtoken==alftoken)||(typtoken==numtoken)) {
  263.                 pcom();
  264.                   if (ctype == (int) c_lnull) {
  265.                           ok = false;
  266.                           eh = true;
  267.                           }
  268.             else doit();
  269.             }
  270.         else if (typtoken==cmarktoken) ;  /* do nothing */
  271.  
  272.         else {
  273.             ok = false;
  274.             eh = true;
  275.             }
  276.         if (ok) ptoken(point);
  277.         else if (eh) writeeh(ppoint);
  278.                 }
  279. }
  280.  
  281.  
  282. pbreak()
  283. {
  284.     cprintf("\r\n");
  285. }
  286.  
  287. doit()
  288. {
  289. int i,j,k,ii;
  290. unsigned int pnumt;
  291. char com_out[255];
  292. unsigned char cmdnum, cmdtask;
  293. unsigned int  com_out_l;
  294.  
  295.         switch (ctype) {
  296.           case c_lnull:
  297.               ok = false;
  298.               eh = true;
  299.         break;
  300.       case c_circuit:
  301.         if (start_report()) break;
  302.         report_circuit();
  303.         end_report();
  304.         break;
  305.       case c_help:
  306.         ptoken(point);
  307.              printf("Commands are:\n");
  308.          i=(numcom)/8;
  309.          k=1;
  310.          for (j=0;j<i;j++) {
  311.             for (ii=0;ii<8;ii++,k++) printf("%.8s ",comtext[k].c);
  312.             printf("\n");
  313.             }
  314.          for (ii=0;ii<((numcom) % 8);ii++,k++)
  315.             printf("%.8s ",comtext[k].c);
  316.          printf("\n");
  317.          break;
  318.       case c_input:
  319.         ptoken(point);
  320.         if ((typtoken==alftoken)||(typtoken==numtoken) ||
  321.             (typtoken==qtmarktoken)) {
  322.             token[tc+1]='\0';
  323.             if (in_open) {
  324.                 fclose(fin);
  325.                 in_open=0;
  326.                 }
  327.             if ((fin = fopen(token,"r")) == NULL){
  328.                 perror("cannot open input file");
  329.                 return;
  330.                 }
  331.             else in_open=1;
  332.             }
  333.         else peh();
  334.         break;
  335.       case c_output:
  336.         ptoken(point);
  337.         if ((typtoken==alftoken)||(typtoken==numtoken) ||
  338.             (typtoken==qtmarktoken)) {
  339.             token[tc+1]='\0';
  340.             if (out_open) {
  341.                 fclose(fout);
  342.                 out_open=0;
  343.                 }
  344.             if ((fout = fopen(token,"w")) == NULL){
  345.                 perror("cannot open output file");
  346.                 return;
  347.                 }
  348.             else out_open=1;
  349.             }
  350.         else peh();
  351.         break;
  352.  
  353.  
  354.       case c_end:
  355.       case c_quit:
  356.         if ((out_open) && (fout != stdout)){
  357.               if (fclose(fout)==EOF) {
  358.                 perror("Error closing output file");
  359.                 }
  360.             }
  361.         exit(0);
  362.         break;
  363.       case c_rr:
  364.         if (start_report()) break;
  365.         report_rr();
  366.         end_report();
  367.         break;
  368.  
  369.       case c_raw:
  370.         ptoken(point);    
  371.         if ((typtoken==notoken) || (typtoken==cmarktoken)) i=0;
  372.         else if (toupper(*token)!='T') i=1;
  373.         if (start_report()) break;
  374.         report_raw(i);
  375.         end_report();
  376.         break;
  377.       case c_record:
  378.         ptoken(point);
  379.         if ((typtoken==notoken) || (typtoken==cmarktoken)) {
  380.             cprintf("Record types selected: %s\r\n",sel_types);
  381.             }
  382.         else {
  383.             strncpy(sel_types,token,sizeof(sel_types));
  384.             sel_types[sizeof(sel_types)-1]='\0';
  385.             strupr(sel_types);
  386.             }
  387.         break;
  388.       case c_select:
  389.         ptoken(point);    
  390.         if ((typtoken==notoken) || (typtoken==cmarktoken)) sel_flag=0;
  391.         else {
  392.             strcpy(sel_call,strupr(token));
  393.             sel_flag=1;
  394.             }
  395.         break;
  396.       default: 
  397.         cprintf("*** internal error, parser default case\n");
  398.         break;
  399.     }
  400.     if(typtoken==cmarktoken) point--;
  401. }
  402. int start_report()
  403. {
  404.     if (!in_open) {
  405.         if ((fin = fopen("log","r")) == NULL){
  406.             perror("cannot open log file");
  407.             return(1);
  408.             }
  409.         else in_open=1;
  410.         }
  411.     if (!out_open) fout=stdout;
  412.     out_open=1;
  413.     return(0);
  414.  
  415. }
  416. end_report()
  417. {
  418.     rewind(fin);
  419.     recnum=0;
  420. }
  421.  
  422.